package com.greenant.store.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.greenant.store.AntHomeApplication;
import com.greenant.store.R;
import com.greenant.store.activity.EmrecvDetailActivity;
import com.greenant.store.activity.OrderDetailActivity;
import com.greenant.store.bean.EMRecv;
import com.greenant.store.bean.Order;
import com.greenant.store.bean.OrderItem;
import com.greenant.store.bean.StoreInfo;
import com.greenant.store.utils.Const;
import com.greenant.store.utils.UtilTools;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

public class EmrecvInportListAdapter extends BaseAdapter {

	private List<EMRecv> list;
	private Context context;
	// BitmapUtils bitmapUtils;
	private final static String TAG = "MyEmrecvListAdapter";

	String cash_on_delivery = "";
	DecimalFormat df = new DecimalFormat("#########.##");

	public EmrecvInportListAdapter(List<EMRecv> tlist, Context ctx) {
		this.list = tlist;
		this.context = ctx;
		cash_on_delivery = context.getString(R.string.order_ensure_pay_method_cash_on_delivery);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.item_emrecv_inport, null);
			viewHolder = new ViewHolder();

			viewHolder.emrecvname = (TextView) convertView.findViewById(R.id.emrecvname);
			viewHolder.emrecvorder = (TextView) convertView.findViewById(R.id.emrecvorder);
			viewHolder.time = (TextView) convertView.findViewById(R.id.time);
			
			viewHolder.emrecvImage = (ImageView) convertView.findViewById(R.id.emrecvImage);
			viewHolder.emrecvnamedesc = (TextView) convertView.findViewById(R.id.emrecvnamedesc);
			viewHolder.timeSign = (TextView) convertView.findViewById(R.id.timeSign);
			
			convertView.setTag(viewHolder);
			
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final EMRecv order = list.get(position);

		if(order.getEMCompany()!=null)
		{
		    viewHolder.emrecvname.setText(order.getEMCompany().getCompanyName()); 
		    viewHolder.emrecvnamedesc.setText(order.getEMCompany().getCompanyName()+context.getString(R.string.emrecv_send_company_hint13));
		    ImageLoader.getInstance().displayImage(order.getEMCompany().getCompanyImageUrl(), viewHolder.emrecvImage);
		
		}
		
		viewHolder.emrecvorder.setText(order.getOrderId()); 
		
		Date date = order.getCreatedAt();
		// 时间
		if(date!=null)
		{
			viewHolder.time.setText(DateFormat.format("yyyy-MM-dd HH:mm", date));
			viewHolder.timeSign.setText(context.getString(R.string.emrecv_send_company_hint12,DateFormat.format("yyyy-MM-dd HH:mm", new Date()))  );
		}else{
			//TODO time
			viewHolder.time.setText(DateFormat.format("yyyy-MM-dd HH:mm", new Date()));
			viewHolder.timeSign.setText(context.getString(R.string.emrecv_send_company_hint12,new Date())  );
		}
		
		
		// 跳转到订单详情页面
        convertView.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(context, EmrecvDetailActivity.class);
                Const.emrecv=order;
                context.startActivity(intent);
            }
        });
        
		return convertView;
	}

	class ViewHolder {
		public TextView emrecvname;
		public TextView emrecvorder;
		public TextView time;
		
		public ImageView emrecvImage;
		public TextView emrecvnamedesc;
		public TextView timeSign;
	}

}
